package com.yanqu.road.server.manger.activity.hongxian;

import com.yanqu.road.dao.impl.activity.hongxian.HongXiuConfigDaoImpl;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.hongxian.config.HongXiuConfig;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.hongxian.HongXianActivityModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;
import java.util.Map;

public class HongXianActivityMgr extends TempMgr {

    private static HongXiuConfig config;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    @Override
    public boolean save() {
        return true;
    }

    public static void reloadActivityData() {
        getLogger().info("reload HongXian Activity start");
        ActivityInfo tempActivityInfo;
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.HongXianActivity.getValue());
        if (openActivityInfoList.isEmpty()) {
            getLogger().info("no HongXian Activity in show time");
            config = null;
        }else {

            tempActivityInfo = openActivityInfoList.get(0);
            int tmpActivityId = tempActivityInfo.getActivityId();

            HongXiuConfig configTemp  = new HongXiuConfig(tempActivityInfo);

            //config配置
            Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(tmpActivityId);

            HongXiuConfigDaoImpl dao = new HongXiuConfigDaoImpl();

            configTemp.setMemoryInfoList(dao.getHongXianMemoryInfoList(tmpActivityId));
            configTemp.setRewardInfoList(dao.getHongXianRewardInfoList(tmpActivityId));
            configTemp.setImageInfoList(dao.getHongXianImageInfoList(tmpActivityId));

            if (activityConfigMap.containsKey(eActivityConfigType.EXCHANGE_SCORE_ITEM_ID.getName())) {
                configTemp.setEXCHANGE_SCORE_ITEM_ID(activityConfigMap.get(eActivityConfigType.EXCHANGE_SCORE_ITEM_ID.getName()).getIntValue());
            }
            configTemp.setDrawItemId(activityConfigMap.get(eActivityConfigType.HONGXIAN_ITEM_ID.getName()).getIntValue());
            configTemp.setNoticeLimit(activityConfigMap.get(eActivityConfigType.HONGXIAN_NOTICE_LIMIT.getName()).getIntValue());
            configTemp.setTEN_GACHA_AND_SKIP(activityConfigMap.get(eActivityConfigType.HONGXIAN_TEN_GACHA_AND_SKIP.getName()).getIntValue());
            if(activityConfigMap.containsKey(eActivityConfigType.HONGXIAN_NOTICE_SPECIAL.getName())) {
                configTemp.setNoticeSpecialList(StringUtils.stringToIntegerList(activityConfigMap.get(eActivityConfigType.HONGXIAN_NOTICE_SPECIAL.getName()).getValue(), "\\|"));
            }
            config = configTemp;
        }

        getLogger().info("reload HongXian Activity finish");

        // 在线玩家下发数据
        for (GamePlayer gamePlayer : GamePlayerMgr.getAllOnlinePlayer()) {
            HongXianActivityModule module = gamePlayer.getModule(HongXianActivityModule.class);
            module.activityReload();
        }
    }

    public static ActivityInfo getActivityInfo() {
        if(config == null) {
            return null;
        }
        return config.getActivityInfo();
    }

    public static int getActivityId(){
        if(config == null){
            return 0;
        }
        return config.getActivityInfo().getActivityId();
    }

    public static HongXiuConfig getConfig() {
        return config;
    }
}
